/**
 * Created by machan on 2017/6/14.
 */

import * as userUtils from './UserUtils';
import * as umfUtils from '../utils/UmfUtils';
import * as constants from '../common/Constants';
import { saveUserCache } from '../model/CacheUtils';
import UmfLog from '../utils/UmfLog';

export default class LoginUtils {

    /**
     * 缓存登录信息
     * @private
     * @param app
     * @param loginData
     */
    static saveLoginData(app, loginData) {
        //[审核账号 特殊处理]
        if (constants.isWeAudit
            && loginData.assistId === constants.weAuditUser.assistId
            && loginData.userId === constants.weAuditUser.userId) {
            loginData.agentLevel = "1";
        }

        //保存用户数据
        userUtils.saveUserInfo(loginData);
        //保存session
        umfUtils.setStorageSync(constants.Keys.SEESION, loginData["token"]);
        //全局userData赋值
        app.globalData.userData = new userUtils.userInfo().get();
        //全局userCache赋值
        saveUserCache();
    }

    /**
     * 清空登录信息
     * @returns {boolean}
     */
    static clearLoginData(){
        try {
            wx.removeStorageSync(constants.Keys.USER_INFO);
            wx.removeStorageSync(constants.Keys.SEESION);
            return true;
        } catch (e) {
            UmfLog.error("清空用户数据、session异常");
        }
        return false;
    }
}